package com.wenjiang.controller;

import com.wenjiang.common.annotation.OperatorLog;
import com.wenjiang.common.dto.PageParam;
import com.wenjiang.common.lang.Result;
import com.wenjiang.entity.MallCarousel;
import com.wenjiang.service.MallCarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/carousel")
public class CarouselController {

    @Autowired
    MallCarouselService mallCarouselService;

    // 获取轮播图
    @RequestMapping("/get")
    public Object getAllCarousel(@RequestBody PageParam pageParam){
        Result result = mallCarouselService.getPageCarousels(pageParam);
        return result;
    }


    //获取轮播图总数
    @RequestMapping("/count")
    public  Object getCount(){
        return Result.succ(mallCarouselService.count());
    }
    // 修改轮播图的状态
    @RequestMapping("/changeStatus/{carouselId}/{status}")
    public Object changeStatus(@PathVariable("carouselId") int carousel,@PathVariable("status")int status){
        Result result = mallCarouselService.changeStatus(carousel, status);
        return result;
    }

    // 修改轮播图
    @RequestMapping("/update")
    @OperatorLog
    public Object update(@RequestBody  MallCarousel mallCarousel){
        Result result = mallCarouselService.updateCarousel(mallCarousel);
        return result;
    }

    // 添加轮播图
    @RequestMapping("/add")
    @OperatorLog
    public Object add(@RequestParam("file")MultipartFile file, HttpServletRequest request){
        MallCarousel mallCarousel = new MallCarousel();
        mallCarousel.setCarouselName(request.getParameter("carouselName"));
        mallCarousel.setCarouselRedirectUrl(request.getParameter("carouselRedirectUrl"));
        mallCarousel.setCarouselRank(Integer.parseInt(request.getParameter("carouselRank")));

        Result result = mallCarouselService.addCarousel(mallCarousel, file);
        return result;
    }

    // 删除轮播图
    @RequestMapping("/del/{carouselId}")
    @OperatorLog
    public Object delete(@PathVariable("carouselId") Integer carouselId){
        Result result = mallCarouselService.delCarousel(carouselId);
        return result;
    }
}
